use std::fs;
use std::sync::LazyLock;
use serde::Deserialize;

#[derive(Deserialize, Debug, Clone)]
pub struct Auth {
    pub app_id: String,
    pub client_secret: String
}

#[derive(Deserialize, Debug, Clone)]
pub struct Mysql {
    pub host: String,
    pub port: i32,
    pub database: String,
    pub username: String,
    pub password: String,
}

#[derive(Deserialize, Debug, Clone)]
pub struct GaodeMap {
    pub key: String
}

#[derive(Deserialize, Debug, Clone)]
pub struct Config {
    auth: Auth,
    mysql: Mysql,
    gaode_map: GaodeMap
}

impl Config {
    pub fn get_mysql(&self) -> Mysql {
        self.mysql.clone()
    }

    pub fn get_gaode_map(&self) -> GaodeMap {
        self.gaode_map.clone()
    }
}

pub fn init() -> Config {
    let content = fs::read_to_string("config.toml").expect("读取配置文件失败，请检查配置文件是否存在！");
    let config: Config = toml::from_str(content.as_str()).expect("读取配置文件失败，请检查配置文件格式是否符合要求！");
    config
}

pub fn get_auth() -> Auth {
    init().auth
}

pub static CONFIG: LazyLock<Config> = LazyLock::new(init);